/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.gadgets.docsFolder.client;



import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.google.DocsFolderItem;
import com.gadglet.client.gwt.google.GoogleRequest;
import com.gadglet.client.gwt.ui.HomeViewMainPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DocsList  extends GoogleRequest{
	final PreferencesFeature  prefs = PreferencesProvider.get();
	final VerticalPanel holder = new VerticalPanel();
	///final HomeViewMainPanel mainPanel = new HomeViewMainPanel();
	
	
	VerticalPanel documentListPanel ;
	
	DocsList( HomeViewMainPanel mainPanel){
		mainPanel.add(holder);
		Button changeFolder = new Button(prefs.getMsg("gadgetLabelPrevious"));
		documentListPanel = new VerticalPanel();
	    holder.add(changeFolder);
	    changeFolder.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				holder.setVisible(false);
				DocsFolderGadget.DF.init(); 
			}
		});
	    
	    holder.setSpacing(10);
	    holder.setVisible(false);
	}
	void init(){
		holder.setVisible(true);
		GadgetNativeUtils.setTitle(prefs.getString("selectedFolderTitle"));
			
	    getDocs();
	}
	
	void getDocs()
	{
		documentListPanel.clear();
		itemsList.clear();
		String selected = prefs.getString("selectedFolder");
		String feedUrl =  "https://docs.google.com/feeds/folders/private/full/";
		selected = selected.substring(selected.indexOf("folder%3A"));
		feedUrl = feedUrl + selected +"?alt=json";
		
		setFeedUrl(feedUrl);
		setHeaders("GData-Version","2.0");
		setAuthorizationMsg("gadgetMsgPleaseApproveDocs");
		callFeed();
		
	}
	@Override
	protected void  processResults(){
	
		
		
		
		Label emptyFolder = new Label(prefs.getMsg("gadgetLabelemptyFolder"));
		emptyFolder.setStyleName("errorMsg");
		DocsFolderItem dh = null;
	
		HorizontalPanel  docRecord = null; 
		
		Anchor doc = null;
		Image docIcon = null;
		
		
		if(itemsList.size() >0){
			for (int y =0; y< itemsList.size(); y++){
			  docRecord = new HorizontalPanel();
			  docRecord.setSpacing(3);
			  dh = new DocsFolderItem(itemsList.get(y));
			  
			  docIcon = new Image(GadgetUtils.getGadgetIconURL(dh.getDocType(), null, false));
			  docRecord.add(docIcon);
			  
			  doc = new Anchor();
			  doc.setHTML(dh.getTitle());
			  doc.setHref(dh.getDocURL());
	    	  doc.setTarget("_blank");
	    	
	    	  docRecord.add(doc);
	    	 
	    	  documentListPanel.add(docRecord);
	      }
		}
		else
			documentListPanel.add(emptyFolder);
	   	      
	  	holder.add(documentListPanel);
	}
}
